package redis

import (
	"github.com/go-redsync/redsync/v4"
	"github.com/go-redsync/redsync/v4/redis/goredis/v9"
	"github.com/redis/go-redis/v9"
	goredislib "github.com/redis/go-redis/v9"
)

type RedisClient struct {
	Client *redis.Client
}

func NewRedisClient() *RedisClient {
	client := goredislib.NewClient(&goredislib.Options{
		Addr: "192.168.2.9:6379",
	})
	return &RedisClient{Client: client}
}

// Mutex 获取分布式锁锁
func (r *RedisClient) Mutex(name string) *redsync.Mutex {
	pool := goredis.NewPool(r.Client)
	rs := redsync.New(pool)
	mutexName := "lock" + name
	mutex := rs.NewMutex(mutexName)
	return mutex
	//if err := mutex.Lock(); err != nil {
	//	logx.Errorf("redis mutex lock error: %v", err)
	//	return
	//}
	//// Do your work that requires the lock.
	//
	//// Release the lock so other processes or threads can obtain a lock.
	//if ok, err := mutex.Unlock(); !ok || err != nil {
	//	panic("unlock failed")
	//}
}
